﻿namespace FFF.Test.Util.WxOpen
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Senparc.Weixin.Entities;
    using Senparc.Weixin.WxOpen.AdvancedAPIs.WxApp;
    using Senparc.Weixin;
    using Senparc.Weixin.Entities.TemplateMessage;
    using Senparc.Weixin.WxOpen.AdvancedAPIs.Template;
    using Senparc.Weixin.MP.CommonAPIs;

    /// <summary>
    /// 微信小程序帮助类
    /// </summary>
    public static class WxOpenHelper
    {
        /// <summary>
        /// 获取小程序短信跳转URL
        /// </summary>
        /// <code>
        /// <![CDATA[
        /// <a href="weixin://dl/business/?t=2KBNkbdfRfi"> 
        /// location.href = 'weixin://dl/business/?t=E1EvMrNAEdi'
        /// ]]>
        /// </code>
        /// <param name="path">The path.</param>
        /// <param name="query">The query.</param>
        /// <param name="isExpire">是否过期</param>
        /// <returns>System.String.</returns>
        /// <remarks>于大明 2019-07-17</remarks>
        public static string GetMsgUrl(string path, string query, bool isExpire)
        {
            string appId = Config.SenparcWeixinSetting.WxOpenAppId;
            GenerateSchemeJumpWxa jumpWxa = new GenerateSchemeJumpWxa(path, query);
            var result = UrlSchemeApi.GenerateScheme(appId, jumpWxa, isExpire);
            if (result.errcode == ReturnCode.请求成功)
            {
                return result.OpenLink;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <code>
        /// <![CDATA[
        /// WxOpenHelper.GenQrCode("pages/index/index", "GK0900000001", "~/down/qrcode.jpg");
        /// ]]>
        /// </code>
        /// <param name="url">小程序地址</param>
        /// <param name="scene">地址携带的参数</param>
        /// <param name="filePath">保存的文件地址(~/down/qrcode.jpg)</param>
        /// <returns>是否生成成功</returns>
        public static bool GenQrCode(string url, string scene, string filePath)
        {
            string appId = Config.SenparcWeixinSetting.WxOpenAppId;
            WxJsonResult result = WxAppApi.GetWxaCodeUnlimit(appId, filePath, scene, url, 640, false, new LineColor(100, 20, 30));
            if (result.errcode == ReturnCode.请求成功)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <code>
        /// <![CDATA[
        ///  var data = new { keyword1 = "keyword1", keyword2 = "keyword2", keyword3 = "keyword3", keyword4 = "keyword4", keyword5 = "keyword5" };
        ///  WxOpenHelper.SendTemplateMessage("oXKH05dyMRIBZDlhBCdgxyrhoB58", "kwooxBs1B-F2W0864_-LS8Z-3xojVd0u-GTJrm1HDu4", "formId", data, "pages/index/index", "keyword1");
        ///  "data": {
        ///                   "keyword1": {
        ///                       "value": "339208499", 
        ///                       "color": "#173177"
        ///                   }, 
        ///                   "keyword2": {
        ///                       "value": "2015年01月05日 12:30", 
        ///                       "color": "#173177"
        ///                   }, 
        ///                   "keyword3": {
        ///                       "value": "粤海喜来登酒店", 
        ///                       "color": "#173177"
        ///                   } , 
        ///                   "keyword4": {
        ///                       "value": "广州市天河区天河路208号", 
        ///                       "color": "#173177"
        ///                   } 
        ///     }
        /// ]]>
        /// </code>
        /// <param name="openId">用户的OpenID</param>
        /// <param name="templateId">模板的ID</param>
        /// <param name="formId">提交的表单的ID</param>
        /// <param name="data">发送的data对象 要构造对应的keyworld </param>
        /// <param name="pageUrl">需要跳转的小程序的URl</param>
        /// <param name="emphasisKeyword">需要放大的关键字</param>
        /// <returns>是否发送成功</returns>
        public static bool SendTemplateMessage(string openId, string templateId, string formId, dynamic data, string pageUrl = null, string emphasisKeyword = null)
        {
            string appId = Config.SenparcWeixinSetting.WxOpenAppId;
            var result = TemplateApi.SendTemplateMessage(appId, openId, templateId, data, formId, pageUrl, emphasisKeyword);
            if (result.errcode == ReturnCode.请求成功)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

}
